@extends('layouts.default') @section('main')
    <link rel="stylesheet" href="{{URL::asset('js/')}}/kindeditor-4.1.7/themes/default/default.css" />
    <link rel="stylesheet" href="{{URL::asset('js/')}}/kindeditor-4.1.7/plugins/code/prettify.css" />
    <script src="{{URL::asset('js/')}}/kindeditor-4.1.7/kindeditor.js" type="text/javascript"></script>
    <script charset="utf-8" src="{{URL::asset('js/')}}/kindeditor-4.1.7/lang/zh_CN.js"></script>
    <script charset="utf-8" src="{{URL::asset('js/')}}/kindeditor-4.1.7/plugins/code/prettify.js"></script>
    <script>
        KindEditor.ready(function(K) {

            var editor1 = K.create('#post_content', {
                uploadJson: "{{url('kindedit/upload')}}",
                fileManagerJson: "{{url('kindedit/manager')}}",
                allowFileManager: true,
                afterBlur: function() {
                    this.sync();
                },
                items: ['source', '|', 'undo', 'redo', '|', 'cut', 'copy', 'paste', 'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript', 'superscript', 'clearhtml', 'quickformat', 'selectall', 'image', '/', 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'table', 'hr', 'emoticons', 'pagebreak', 'anchor', 'link', 'unlink', '|']
            });

        });
    </script>
    <div id="content">
        <div id="content-header">
            <div id="breadcrumb">
                <a href="{{url('home')}}" title="返回首页" class="tip-bottom"><i
                            class="icon-home"></i> 首页</a>
                <a href="{{url('topic/index')}}" class="tip-bottom">专题管理</a>
                <a href="{{url('topicpost/index?id='.$topic_id)}}" class="tip-bottom">关联管理</a>
                <a href="#" class="current">新增关联</a>
            </div>
        </div>
        <div class="container-fluid">
            <div class="row-fluid">
                <div class="span9">
                    <div class="widget-box">
                        <div class="widget-title">
						<span class="icon"> <i class="icon-align-justify"></i>
						</span>
                            <h5>新增关联</h5>
                        </div>
                        <div class="widget-content nopadding">
                            <form action="{{url('topicpost/create')}}" method="post" id="adForm" class="form-horizontal">

                                <div class="control-group">
                                    <label class="control-label"><span class="redD">*</span>标题 :</label>
                                    <div class="controls">
                                        <input type="text" class="span11" name="subject" placeholder="标题"  />
                                    </div>
                                </div>


                                <div class="control-group">
                                    <label class="control-label"><span class="redD">*</span>内容 :</label>
                                    <div class="controls">
                                        <textarea id="post_content" placeholder='内容' name="content" style="width: 450px; height: 240px;"></textarea>
                                    </div>
                                </div>


                                <div class="control-group ">
                                    <label class="control-label"><span class="redD">*</span>类型 :</label>
                                    <div class="controls ">
                                        <select  name="type" class=" span11" >
                                            <option value="">请选择</option>
                                            <option value="maker">创客空间</option>
                                            <option value="investor">投资者</option>
                                            <option value="video">视频</option>
                                            <option value="project">项目</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="control-group">
                                    <label class="control-label"><span class="redD">*</span>关联目标标题的一部分(供搜索) :</label>
                                    <div class="controls">
                                        <input type="text" class="span11" name="target_search" placeholder="输入关联目标标题一部分，按enter键搜索，若为投资者，则输入username一部分" />
                                    </div>
                                </div>

                                <div class="control-group">
                                    <label class="control-label"><span class="redD">*</span>关联的目标 :</label>
                                    <div class="controls">
                                        <select name="post_id" id="post_id">
                                            <option value="0"></option>
                                            {{--@foreach( $users as $key => $user )--}}
                                            {{--<option value="{{ $user->uid }}">{{ $user->realname or $user->nickname }}</option>--}}
                                            {{--@endforeach--}}
                                        </select>
                                    </div>
                                </div>

                                <div class="control-group">
                                    <label class="control-label">排序 :</label>
                                    <div class="controls">
                                        <input type="text" class="span11" name="sort" placeholder="sort" value="0"  />
                                    </div>
                                </div>

                                <input type="hidden" class="span11" name="formType" value="create" />
                                <div class="form-actions">
                                    <input type="hidden" name="_token" value="{{ csrf_token() }}">
                                    <input type="hidden" name="topic_id" value="{{$topic_id}}">
                                    <button type="button" id="doSave" class="btn btn-success">保存</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>

        $("input[name='target_search']").keydown(function(event) {
            if (event.which == "13"){
                var param = {};
                param['target_search'] = $(this).val();
                param['type'] = $("select[name='type']").val();

                ajaxRequest(param, '/topic/target-search', function (data) {
                    if (data.status) {
                        var html = '<option value="">请选择</option>';
                        if(data.message.type=='investor'){
                            $.each(data.message.lists, function (i, n) {
                                html += "<option value='" + n.uid + "'>" + n.username + "</option>";
                            });
                        }else if(data.message.type=='maker'){
                            $.each(data.message.lists, function (i, n) {
                                html += "<option value='" + n.id + "'>" + n.name + "</option>";
                            });
                        }else{
                            $.each(data.message.lists, function (i, n) {
                                html += "<option value='" + n.id + "'>" + n.subject + "</option>";
                            });                        }
                        $("#post_id").html(html);
                        alert('恭喜，已经生成选项,请选择所需要的目标');
                    } else {
                        var html = '<option value="">请按上面输入框的说明生成选项</option>';
                        $("#post_id").html(html);
                        alert(data.message);
                    }
                });
            }
        });

        $("#doSave").click(function(){
            $(this).attr('disabled','disabled');

            ajaxRequest($('#adForm').serializeArray(),$('#adForm').attr('action'),function(data){
                if(data.status){
                    alert(data.message);
                    window.location.href = data.forwardUrl;
                }else{
                    $("#doSave").removeAttr('disabled');

                    alert(data.message);
                }
            });
        });
    </script>

    <script type="text/javascript" src="{{URL::asset('js/')}}/ajaxfileupload.js"></script>
    <script type="text/javascript" src="{{URL::asset('js/')}}/common.js"></script>
@stop
